import { useResponseSuccess } from '~/utils/response';

export default eventHandler(async (event) => {
  const query = getQuery(event);
  const { keyword, templateType, status, page = 1, pageSize = 10 } = query;

  // 模拟模板数据
  const templates = [
    {
      id: '1',
      name: '标准客房模板',
      code: 'ROOM_TEMPLATE_001',
      templateType: 'PHYSICAL',
      description: '适用于酒店标准客房的资源模板',
      version: '1.0.0',
      resourceCount: 8,
      status: 'ACTIVE',
      createTime: '2024-01-15 10:30:00',
      updateTime: '2024-01-15 10:30:00',
    },
    {
      id: '2',
      name: '温泉池模板',
      code: 'SPA_TEMPLATE_001',
      templateType: 'PHYSICAL',
      description: '适用于温泉洗浴的池子资源模板',
      version: '1.0.0',
      resourceCount: 5,
      status: 'ACTIVE',
      createTime: '2024-01-15 09:15:00',
      updateTime: '2024-01-15 09:15:00',
    },
    {
      id: '3',
      name: '餐厅包间模板',
      code: 'DINING_TEMPLATE_001',
      templateType: 'PHYSICAL',
      description: '适用于餐厅包间的资源模板',
      version: '1.0.0',
      resourceCount: 6,
      status: 'ACTIVE',
      createTime: '2024-01-15 08:45:00',
      updateTime: '2024-01-15 08:45:00',
    },
    {
      id: '4',
      name: '预约表单模板',
      code: 'FORM_TEMPLATE_001',
      templateType: 'TECHNICAL',
      description: '通用预约表单模板',
      version: '1.0.0',
      resourceCount: 3,
      status: 'ACTIVE',
      createTime: '2024-01-14 16:20:00',
      updateTime: '2024-01-14 16:20:00',
    },
    {
      id: '5',
      name: '资源报表模板',
      code: 'REPORT_TEMPLATE_001',
      templateType: 'TECHNICAL',
      description: '资源使用情况报表模板',
      version: '1.0.0',
      resourceCount: 4,
      status: 'ACTIVE',
      createTime: '2024-01-14 15:10:00',
      updateTime: '2024-01-14 15:10:00',
    },
  ];

  // 过滤数据
  let filteredData = templates;

  if (keyword) {
    filteredData = filteredData.filter(
      (item) =>
        item.name.includes(keyword as string) ||
        item.code.includes(keyword as string),
    );
  }

  if (templateType) {
    filteredData = filteredData.filter(
      (item) => item.templateType === templateType,
    );
  }

  if (status) {
    filteredData = filteredData.filter(
      (item) => item.status === status,
    );
  }

  // 分页
  const start = (Number(page) - 1) * Number(pageSize);
  const end = start + Number(pageSize);
  const items = filteredData.slice(start, end);

  return useResponseSuccess({
    items,
    total: filteredData.length,
    page: Number(page),
    pageSize: Number(pageSize),
  });
});
